package com.iflytek.yly.jetpackdemo.databinding;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;

import com.iflytek.yly.jetpackdemo.BR;

public class TwoWayBindingViewModel extends BaseObservable {

    private LoginModel loginModel;

    public TwoWayBindingViewModel(){
        loginModel = new LoginModel();
        loginModel.userName = "Chris";
    }

    @Bindable
    public String getUserName(){
        return loginModel.userName;
    }

    public void setUserName(String userName) {

        if(userName != null && !userName.equals(loginModel.userName)){
            loginModel.userName = userName;
            //可以在此处理一些与业务相关的逻辑，例如保存userName字段
            notifyPropertyChanged(BR.userName);
        }
    }
}
